//
//  BaseCell.swift
//  CellAutolayoutHeight
//
//  Created by zk_01 on 2022/3/29.
//

import UIKit
import Kingfisher

class BaseCell: UITableViewCell {
    
    internal var model:DataModel?
    
    /// 肖像的宽
    let portraitImgView_W = 45.0
    
    /// 名称距离肖像的边距
    let nameLab_LeftMargin = 8.0
    
    //肖像左边的边距
    let portraitImgView_Margin = 10.0
    
    enum EventType {
        case void
        case contentCollapse(Bool)
    }
    
    typealias Event = (UITableViewCell,DataModel,EventType)->Void
    
    var events:Event?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.portraitImgView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10.0)
            make.left.equalToSuperview().offset(self.portraitImgView_Margin)
            make.width.equalTo(self.portraitImgView_W)
            make.height.equalTo(self.portraitImgView_W)
        }

        self.nameLab.snp.makeConstraints { make in
            make.top.equalTo(self.portraitImgView)
            make.left.equalTo(self.portraitImgView.snp.right).offset(self.nameLab_LeftMargin)
            make.right.equalToSuperview().offset(-10.0)
            make.height.equalTo(22.0)
        }
        
        self.timeLab.snp.makeConstraints { make in
            make.top.equalTo(self.nameLab.snp.bottom)
            make.left.equalTo(self.nameLab)
            make.right.equalTo(self.contentView.snp.centerX)
            make.bottom.equalTo(self.portraitImgView.snp.bottom)
        }
        
        self.bottomBarStackView.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-10)
            make.centerX.equalToSuperview()
            make.width.equalTo(self.contentView.frame.width * 4 / 5.0)
            make.height.equalTo(35.0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    /// 赋值
    func setModel(_ model:DataModel){
        self.model = model
        //下载图片，动画显示
        self.portraitImgView.kf.setImage(with: ImageResource.init(downloadURL: URL(string: model.portraitUrl)!),
                                       placeholder: UIImage(named: "portraitImage"),
                                       options:  [.transition(ImageTransition.fade(1))],
                                       progressBlock: { (receivedSize, totalSize) in
        }) { res in
            
        }
        self.nameLab.text = model.id + ":" + model.name
        self.timeLab.text = model.timeStr
        let imgName = model.isLike ? "circle_circle_likeSelect":"circle_circle_likeNormal"
        (self.bottomBarStackView.subviews as! [MButton]).filter({ $0.type == .like }).first?.setImage(UIImage(named: imgName), for: .normal)
    }
    
    @objc func bottomBarBtnClick(_ sender:MButton){
        switch sender.type{
        case .shared:
            print("分享")
            break
        case .comment:
            print("评论")
            break
        case .like:
            print("点赞")
            guard let model = self.model else { return  }
            model.isLike = !model.isLike
            let imgName = model.isLike ? "circle_circle_likeSelect":"circle_circle_likeNormal"
            sender.setImage(UIImage(named: imgName), for: .normal)
            break
        }
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    lazy var portraitImgView: UIImageView = {
        let imgV = UIImageView()
        imgV.contentMode = .scaleAspectFill
        self.contentView.addSubview(imgV)
        return imgV
    }()
    
    lazy var nameLab: UILabel = {
        let lab = UILabel()
        lab.textAlignment = .left
        lab.font = .systemFont(ofSize: 18.0)
        self.contentView.addSubview(lab)
        return lab
    }()
    
    lazy var timeLab: UILabel = {
        let lab = UILabel()
        lab.textColor = .gray
        lab.textAlignment = .left
        lab.font = .systemFont(ofSize: 13.0)
        self.contentView.addSubview(lab)
        return lab
    }()
    
    
    lazy var hotTopicsStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [UIView]())
        stackView.axis = .vertical
        stackView.spacing = 8.0
        stackView.distribution = .fillEqually
        self.contentView.addSubview(stackView)
        return stackView
    }()
    
    lazy var bottomBarStackView: UIStackView = {

        let stackView = UIStackView(arrangedSubviews: [UIView]())
        stackView.axis = .horizontal
        stackView.distribution = .equalCentering
        self.contentView.addSubview(stackView)
        [("circle_circle_share",MButton.MButtonType.shared),("circle_circle_comments",MButton.MButtonType.comment),("circle_circle_likeNormal",MButton.MButtonType.like)].forEach({ (imgName,type) in
            let btn = MButton()
            btn.type = type
            btn.setImage(UIImage(named: imgName), for: .normal)
            btn.size = CGSize(width: 40.0, height: 18.0)
            btn.addTarget(self, action: #selector(bottomBarBtnClick(_:)), for: .touchUpInside)
            stackView.addArrangedSubview(btn)
        })
        return stackView
    }()
}

class MButton:UIButton{
    enum MButtonType {
        case shared
        case comment
        case like
    }
    var type:MButtonType = .shared
}
